﻿using System;
using System.Windows.Media;

namespace ServerCrafter.ClassLibrary
{
    /// <summary>
    /// Stores all ServerCrafter settings.
    /// </summary>
    public static class Settings
    {
        /// <summary>
        /// Load settings into variables.
        /// </summary>
        public static class Load
        {
            public static void All()
            {
                Logger.WriteInfo("Loading all settings.");
                NullSettings();
                ServerOutputColors();
                ServerStartInfo();
                Logger.WriteInfo("Finished loading all settings.");
            }

            private static void NullSettings()
            {
                //if (Properties.Settings.Default.ServerStartInfo_FileName == null)
                Properties.Settings.Default.ServerStartInfo_FileName =
                    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                    "\\ServerCrafter\\CraftBukkit.jar";
                if (Properties.Settings.Default.ServerStartInfo_WorkingDirectory == null)
                    Properties.Settings.Default.ServerStartInfo_WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
            }

            private static void ServerOutputColors()
            {
                Logger.WriteInfo("Loading ServerOutputColors.");
                Settings.ServerOutputColors.Info =
                    Properties.Settings.Default.ServerOutputColors_Info;
                Settings.ServerOutputColors.Warning =
                    Properties.Settings.Default.ServerOutputColors_Warning;
                Settings.ServerOutputColors.Severe =
                    Properties.Settings.Default.ServerOutputColors_Severe;
                Settings.ServerOutputColors.PlayerJoin =
                    Properties.Settings.Default.ServerOutputColors_PlayerJoin;
                Settings.ServerOutputColors.PlayerLeave =
                    Properties.Settings.Default.ServerOutputColors_PlayerLeave;
                Settings.ServerOutputColors.Chat =
                    Properties.Settings.Default.ServerOutputColors_Chat;
                Settings.ServerOutputColors.ServerCrafter =
                    Properties.Settings.Default.ServerOutputColors_ServerCrafter;
                Settings.ServerOutputColors.Unknown =
                    Properties.Settings.Default.ServerOutputColors_Unknown;
                Logger.WriteInfo("Finished loading ServerOutputColors.");
            }

            private static void ServerStartInfo()
            {
                Logger.WriteInfo("Loading ServerStartInfo.");
                Settings.ServerStartInfo.FileName =
                    Properties.Settings.Default.ServerStartInfo_FileName;
                Settings.ServerStartInfo.Arguments =
                    Properties.Settings.Default.ServerStartInfo_Arguments;
                Settings.ServerStartInfo.WorkingDirectory =
                    Properties.Settings.Default.ServerStartInfo_WorkingDirectory;
                Settings.ServerStartInfo.InitialRam =
                    Properties.Settings.Default.ServerStartInfo_InitialRam;
                Settings.ServerStartInfo.MaximumRam =
                    Properties.Settings.Default.ServerStartInfo_MaximumRam;
                Logger.WriteInfo("Finished loading ServerStartInfo.");
            }
        }

        /// <summary>
        /// Apply changes into settings.
        /// </summary>
        public static class Apply
        {
            static Apply()
            {
                ServerOutputColors();
                ServerStartInfo();
            }

            public static void ServerOutputColors()
            {
                Logger.WriteInfo("Applying ServerOutputColors changes.");
                Properties.Settings.Default.ServerOutputColors_Info =
                    Settings.ServerOutputColors.Info;
                Properties.Settings.Default.ServerOutputColors_Warning =
                    Settings.ServerOutputColors.Warning;
                Properties.Settings.Default.ServerOutputColors_Severe =
                    Settings.ServerOutputColors.Severe;
                Properties.Settings.Default.ServerOutputColors_PlayerJoin =
                    Settings.ServerOutputColors.PlayerJoin;
                Properties.Settings.Default.ServerOutputColors_PlayerLeave =
                    Settings.ServerOutputColors.PlayerLeave;
                Properties.Settings.Default.ServerOutputColors_Chat =
                    Settings.ServerOutputColors.Chat;
                Properties.Settings.Default.ServerOutputColors_ServerCrafter =
                    Settings.ServerOutputColors.ServerCrafter;
                Properties.Settings.Default.ServerOutputColors_Unknown =
                    Settings.ServerOutputColors.Unknown;
                Logger.WriteInfo("Finished applying ServerOutputColors changes. Saving changes.");
                Properties.Settings.Default.Save();
                Properties.Settings.Default.Upgrade();
                Logger.WriteInfo("Saved changes to ServerOutputColors.");
            }

            public static void ServerStartInfo()
            {
                Logger.WriteInfo("Applying ServerStartInfo changes.");
                Properties.Settings.Default.ServerStartInfo_FileName =
                    Settings.ServerStartInfo.FileName;
                Properties.Settings.Default.ServerStartInfo_Arguments =
                    Settings.ServerStartInfo.Arguments;
                Properties.Settings.Default.ServerStartInfo_WorkingDirectory =
                    Settings.ServerStartInfo.WorkingDirectory;
                Properties.Settings.Default.ServerStartInfo_InitialRam =
                    Settings.ServerStartInfo.InitialRam;
                Properties.Settings.Default.ServerStartInfo_MaximumRam =
                    Settings.ServerStartInfo.MaximumRam;
                Logger.WriteInfo("Finished applying ServerStartInfo changes. Saving changes.");
                Properties.Settings.Default.Save();
                Properties.Settings.Default.Upgrade();
                Logger.WriteInfo("Saved changes to ServerStartInfo.");
            }
        }

        /// <summary>
        /// Stores colors for server output.
        /// </summary>
        public static class ServerOutputColors
        {
            public static Color Info { get; set; }

            public static Color Warning { get; set; }

            public static Color Severe { get; set; }

            public static Color PlayerJoin { get; set; }

            public static Color PlayerLeave { get; set; }

            public static Color Chat { get; set; }

            public static Color ServerCrafter { get; set; }

            public static Color Unknown { get; set; }
        }

        /// <summary>
        /// Stores variables for server process start info.
        /// </summary>
        public static class ServerStartInfo
        {
            public static string FileName { get; set; }

            public static string Arguments { get; set; }

            public static string WorkingDirectory { get; set; }

            public static int InitialRam { get; set; }

            public static int MaximumRam { get; set; }
        }
    }
}